<template>
  <cube-form
    :model="model"
    :schema="schema"
    :immediate-validate="false"
    @validate="validateHandler"
    @submit="submitHandler"
    @reset="resetHandler"></cube-form>
</template>
<script>
  import { login } from '@/api/login'
  export default {
    data () {
      return {
        model: {
          username: '',
          password: ''
        },
        options: {
          scrollToInvalidField: true,
          layout: 'standard' // classic fresh
        },
        schema: {
          groups: [
            {
              legend: '登录',
              fields: [
                {
                  type: 'input',
                  modelKey: 'username',
                  label: '用户名',
                  props: {
                    placeholder: '请输入用户名',
                    autofocus: true
                  },
                  rules: {
                    required: true
                  },
                  trigger: 'blur'
                },
                {
                  type: 'input',
                  modelKey: 'password',
                  label: '密码',
                  props: {
                    type: 'password',
                    placeholder: '请输入密码',
                    eye: false
                  },
                  rules: {
                    required: true
                  },
                  trigger: 'blur'
                }
              ]
            },
            {
              fields: [
                {
                  type: 'submit',
                  label: '登录'
                },
                {
                  type: 'reset',
                  label: '重置'
                }
              ]
            }
          ]
        }
      }
    },
    methods: {
      submitHandler (e) {
        e.preventDefault()
        // console.log('submit', e)
        login(this.model, (data) => {
          localStorage.setItem('meyouone_crm_userInfo', JSON.stringify(data.result))
          this.$router.push({path: '/sCusList'})
        })
      },
      validateHandler (result) {
        this.validity = result.validity
        this.valid = result.valid
        console.log('validity', result.validity, result.valid, result.dirty, result.firstInvalidFieldIndex)
      },
      resetHandler (e) {
        console.log('reset', e)
      }
    }
  }
</script>
